 
layui.define(['form','xmSelect','tableSelect'], function(exports){
	 var $ = layui.$
	    ,form = layui.form
	    ,setter = layui.setter
	    ,laydate = layui.laydate
	    ,tableSelect = layui.tableSelect
	    ,xmSelect = layui.xmSelect;
	    $("select[name='source']").attr("disabled","disabled");
		form.render('select');
	    var menuSelect = xmSelect.render({
			el: '#menuSelect', 
			autoRow: true,
			name:'menuId',
			filterable:true,
			radio: true,
			clickClose: true,
			tree: {
				show: true,
				showFolderIcon: true,
				showLine: true,
				strict: false,
				indent: 20,
				expandedKeys: [ -3 ],
			},
			height: 'auto',
			on: function(data){
				if(data.isAdd){
//					$('#menuSelect').find("input[name=menuId]").val(ids);
					$('input[name=pId]').val(data.change[0].pId);
				}
			}
		});
	  $.ajax({
		  url: setter.host+'/adminmanager/menu/queryTreeList'
	      ,type:'get'
	      ,dataType:'json'
	      ,success: function(res){
	        //请求成功后
	    	  if(res.status == setter.response.statusCode.status_Success){
	    		  menuSelect.update({
		      		  data: res.data,
		      		  autoRow: true,
		      	  })
	    	  }else{
	    		  layer.msg(res.msg);
	    	  }
	      }
	  });
	    form.on('select(typeSelect)',function (data){
	    	if(data.value=='button'){
	    		$('#url').val('add');
	    		$('#menuName1').find("input").val('新增');
		    	$('#menuName1').css('display','none');
		    	$('#menuName2').css('display','block');
		    	$("select[name='source']").attr("disabled","disabled");
		    	$("select[name='source']").val('self');
		    	$('#selectSource').css('display','none');
		    	form.render('select');
	    	}else if(data.value=='menu'){
	    		$('#url').val('');
	    		$('#menuName1').find("input").val('');
		    	$('#menuName1').css('display','block');
		    	$('#menuName2').css('display','none');
		    	$("select[name='source']").removeAttr("disabled");
		    	form.render('select');
	    	}else {
	    		$('#url').val('');
	    		$('#menuName1').find("input").val('');
		    	$('#menuName1').css('display','block');
		    	$('#menuName2').css('display','none');
		    	$("select[name='source']").attr("disabled","disabled");
		    	$("select[name='source']").val('self');
		    	$('#selectSource').css('display','none');
		    	form.render('select');
	    	}
	    });
	    form.on('select(sourceSelect)',function (data){
	    	if(data.value=='self'){
		    	$('#selectSource').css('display','none');
	    	}else {
		    	$('#selectSource').css('display','block');
	    	}
	    });
	    form.on('select(buttonSelect)',function (data){
            $("select[name='btnName']").next().find("dl").css({ "display": "none" });
            $("input[name='btnName']").val(data.elem[data.elem.selectedIndex].text);
            
            $('#url').val(data.value);
            $('input[name="name"]').val(data.elem[data.elem.selectedIndex].text);
            form.render();
	    });
		$("#icon").click(function(){
			layer.open({
				type: 2,
				title:"图标管理 -- 当前共引入168个内置图标",
				area:['95%', '95%'],
				scrollbar:false,
				content:setter.host+'/adminmanager/menu/toIcons',
			});
			return false;
		});
	    $("#about1").mouseover(function() {
	    	layer.tips('设置该模块下的可见字段', this);
	    });
	    var colsArr = [
		                  { type: 'radio' },
		                  { field: 'moduleId', sort:true,title: '模块ID' },
		                  { field: 'moduleName', title: '模块名称' }
		              ];
		  var tableHead = '';
		  layui.each(colsArr,function(inx,val){
				tableHead += val.hasOwnProperty('field')?val.field+',':',';
		   	})
		   	//tableHead是获取表字段
	   tableHead = tableHead.substring(tableHead.indexOf(',')+1,tableHead.length-1);
		  tableSelect.render({
	       elem: '#modelSelect',
	       checkedKey: 'id',
	       height:'360',
	       width:'400',
	       searchType: 'more',
	       searchList: [
	           {searchKey: 'jobNum', searchPlaceholder: '搜索模块id'}
	       ],
	       table: {
	           url: layui.setter.host+'/adminmanager/menu/loadPlatformModel'
	           ,method:'post'
	           ,where:{'headField':tableHead}
	           ,contentType:'application/json; charset=utf-8'
	        	  ,page: true
		   		  ,groups:2
		   		  ,limit: 10
		   		  ,limits:[10,20,30]
	           ,text: {
		    	    none: '暂无相关数据' 
		    	  }
	           ,cols: [colsArr]
	       },
	       done: function (elem, data) {
	           var resultKey = []
	           var resultValue = []
	           var resultUrl = []
	           var resultField = []
	           layui.each(data.data, function (index, item) {
	        	   resultKey.push(item.moduleName);
	        	   resultValue.push(item.moduleId);
	        	   resultUrl.push(item.moduleUrl);
	        	   resultField.push(item.field);
	           })
	           elem.val(resultKey.join(","));
	           $("input[name='modelValue']").val(resultValue.join(","));
	           $("input[name='url']").val(resultUrl.join(","));
	           $("input[name='name']").val(resultKey.join(","));
	           $("input[name='field']").val(resultField.join(","));
	       }
	   });
  //对外暴露的接口
    exports('insert', {});
});